<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <div v-html="parentHtml"></div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data: ()=>{
    return {
      parentHtml: null
    }
  },
  mounted(){
    //通知父页面加载完成
    let msg = {
      type: 1,
      msg: "子页面已经准备就绪！"
    }
    window.parent.postMessage(msg, "http://localhost:8081");

    //注册事件处理父页面发送消息
    window.addEventListener("message", (event)=>{
      if(event.source == window.parent) {
        let data = event.data;
        console.log("收到父页面发送消息：", data);
        if(data.type == 5) {//父页面发送插入html请求
            this.$data.parentHtml = data.msg;
        }
      }
    })
  },

}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #1650ce;
}
</style>
